package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupItemMapper;
import com.itheima.health.dao.CheckItemMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    CheckItemMapper checkItemMapper;

    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;
    /**
     * @Author: JASON
     * @Date: 10:49 2021/1/4
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     * @Description: checkitem分页查询
    */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.info("Service检查项分页查询"+queryPageBean);
        //分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询
        Page<CheckItem> page = checkItemMapper.findPage(queryPageBean.getQueryString());
        log.info("查询结果为" + page.getResult());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }
    /**
     * @Author: JASON
     * @Date: 11:40 2021/1/4
     * @Parms [checkItem]
     * @ReturnType: void
     * @Description: 新增检查项
    */
    @Override
    public void add(CheckItem checkItem) {
        log.info("Service[检查项]添加数据"+checkItem);
        checkItemMapper.insertSelective(checkItem);
    }

    /**
     * @Author: JASON
     * @Date: 12:16 2021/1/4
     * @Parms [id]
     * @ReturnType: void
     * @Description: 根据id删除检查项
    */
    @Override
    public void delete(Integer id) {
        log.info("Service[检查项]根据id删除数据"+id);
        //删除之前要判断是否被检查组使用
        //需要查询中间关联关系表查询是否引用
        CheckGroupItem checkGroupItem=new CheckGroupItem();
        checkGroupItem.setCheckitemId(id);
        int count = checkGroupItemMapper.selectCount(checkGroupItem);
        if(count>0){
            throw new BusinessException("该检查项有关联数据不可以删除");
        }
        checkItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: JASON
     * @Date: 14:50 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.pojo.CheckItem
     * @Description: 根据id查找检查项
    */
    @Override
    public CheckItem findById(Integer id) {
        log.info("Service[检查项]根据id查找数据"+id);
        return checkItemMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author: JASON
     * @Date: 15:05 2021/1/4
     * @Parms [checkItem]
     * @ReturnType: void
     * @Description: 根据id修改检查项
    */
    @Override
    public void edit(CheckItem checkItem) {
        log.info("Service[检查项]编辑"+checkItem);
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    /**
     * @Author: JASON
     * @Date: 14:43 2021/1/5
     * @Parms []
     * @ReturnType: java.util.List<com.itheima.health.pojo.CheckItem>
     * @Description: 检查项查询所有
    */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectAll();
    }
}
